import {
	getOrderList,
	getDeviceByOrder,
	updateDeviceByOrder,
	inStorageDeviceOther,
	outStorageDeviceOther,
	saleDevice,
	queryIsInStorage,
	deviceInStorage,
	getDeviceByCustomer,
	getDevice,
	getProductList,
	getServiceReport,
	getOrderReport,
	getReportPageList,
	getDetailReportList,
	getQueryOrderReport,
} from '../../api/storageOrder.js'

function resetInfo() {
	let obj = {
		code: '', //机身码
		model: '', //型号
		color: '', //颜色
		shop: '', //上样门店
		createTime: '', //出厂日期
		saleTime: '', //销售日期
		outType: '1', //出库类型
		salePrice: '', //单价
		salor: '导购', //销售人员
		isNewDevice: false, //是否有档案
	}
	return obj
}

export default {
	state: {
		scanDeviceInfo: resetInfo(),
		sumList: [], //数据汇总列表
		canAdd: false, //是否可以添加到列表
		reportList:[], //报备查看列表
	},
	mutations: {
		updateScanDeviceInfo(state, payload) {
			state.scanDeviceInfo = {
				...state.scanDeviceInfo,
				...payload
			}
		},
		resetScanDeviceInfo(state) {
			state.scanDeviceInfo = resetInfo()
			state.canAdd = false
		},
		pushSumList(state) {
			let info = JSON.parse(JSON.stringify(state.scanDeviceInfo))
			state.sumList.push({
				...info,
				checked: true
			})
			state.scanDeviceInfo = resetInfo()
		},
		// 数据汇总
		resetSumList(state) {
			state.sumList = []
		},
		// reportList:[], 报备查看列表
		resetReportList(state) {
			state.reportList=[]
		},
		// 新的List
		dropSumListItem(state) {
			let newArr = []
			state.sumList.forEach((e, i) => {
				if (!e.checked) {
					newArr.push(e)
				}
			})
			state.sumList = newArr
		},
		updateSumListCheck(state, indexArr) {
			state.sumList.forEach((e, i) => {
				if (indexArr.indexOf(`${i}`) > -1) {
					e.checked = true
				} else {
					e.checked = false
				}
			})
		},
		setSumList(state, payload) {
			state.sumList = payload
		},
		// 提交
		updateCanAdd(state, payload) {
			state.canAdd = payload
		}
	},
	getters: {
		checkedListLength(state) {
			let list = []
			state.sumList.forEach(e => {
				if (e.checked) {
					list.push(e)
				}
			})
			return list.length
		}
	},
	actions: {
		getOrderList({}, payload) {
			return new Promise((resolve, reject) => {
				getOrderList(payload).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		getDeviceByOrder({}, payload) {
			return new Promise((resolve, reject) => {
				getDeviceByOrder(payload).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		updateDeviceByOrder({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				updateDeviceByOrder({
					...payload,
					customer: rootState.user.userInfo.teamNo
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		inStorageDeviceOther({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				inStorageDeviceOther({
					...payload,
					customer: rootState.user.userInfo.teamNo
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		outStorageDeviceOther({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				outStorageDeviceOther({
					...payload,
					customer: rootState.user.userInfo.teamNo
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		saleDevice({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				saleDevice({
					...payload,
					customer: rootState.user.userInfo.teamNo
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		queryIsInStorage({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				queryIsInStorage({
					...payload,
					customer: rootState.user.userInfo.teamNo
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		deviceInStorage({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				deviceInStorage({
					...payload,
					customer: rootState.user.userInfo.teamNo
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		getDeviceByCustomer({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				getDeviceByCustomer({
					...payload,
					customer: rootState.user.userInfo.teamNo
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		getDevice({}, payload) {
			return new Promise((resolve, reject) => {
				getDevice({
					...payload,
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		// 判断是否在报备库
		getOrderReport({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				getOrderReport({
					...payload,
					customer:rootState.user.userInfo.teamNo
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		// 发起报备入库
		getServiceReport({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				getServiceReport({
					...payload,
					customer:rootState.user.userInfo.teamNo
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		// 查看报备库列表
		getReportPageList({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				getReportPageList({
					...payload,
					customer: rootState.user.userInfo.teamNo,
					page: 1,
					limit: 100000
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		// 查看详情
		getDetailReportList({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				getDetailReportList({
					...payload,
					customer: rootState.user.userInfo.teamNo,
					page: 1,
					limit: 100000
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		// 根据报备单号查list getQueryOrderReport
		getQueryOrderReport({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				getQueryOrderReport({
					...payload,
					customer: rootState.user.userInfo.teamNo,
					page: 1,
					limit: 100000
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
		getProductList({
			rootState
		}, payload) {
			return new Promise((resolve, reject) => {
				getProductList({
					...payload,
					customer: rootState.user.userInfo.teamNo,
					page: 1,
					limit: 100000
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},
	},
	namespaced: true
}
